from pyrobot.brain import Brain
import CList
import CQueue
import CPilot
class WB (Brain) :
	def setup (self):
		self.counter = 0
		self.inverse = {'right' : 'left', 'down' : 'up', 'left' : 'right', 'up' : 'down'}
		self.actions = CQueue.CQueue()
		self.lastaction = None
		self.backtrace = CList.CList()
		self.pilot = CPilot.CPilot( )
		self.robot.move('reset')
	def wumpusik(self, data):
		if data=="This thing doesn't speak!":
			return False
		return self.actions.Push(data)
		
	def step (self):
		if not(self.robot.getItem('win')):
			self.pilot.setSonar(self.robot.getItem('sonar'))	
			if not(self.robot.getItem('win')):
				if self.robot.getItem('golds')>0:
					if not self.pilot.isCrossRoad():
						self.lastaction=self.pilot.nextMove()
						pos=self.robot.move(self.lastaction)
						if pos=='wumpus':
							while	self.wumpusik(self.robot.move('talk'))!=False:
								pass
						if pos=='gold':
							self.robot.move('grab')
							self.pilot.__init__()
					else:
						pos=self.robot.move(self.pilot.moveTo(self.actions.Pop()))
						self.backtrace.Insert(self.inverse[self.lastaction])
						
				else:
					if not self.pilot.isCrossRoad():
						pos=self.robot.move(self.pilot.nextMove())
						pos
					else:
						pos=self.robot.move(self.pilot.moveTo(self.backtrace.getData()))
						self.backtrace.Remove()
					
			
								
					
						
						
def INIT(engine):
	return WB('WB', engine)